package com.ycz.master.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ycz.master.pojo.Dept;
import com.ycz.master.pojo.Emp;
import com.ycz.master.pojo.EmpAndEmpTypeAndDept;
import com.ycz.master.pojo.EmpType;
import com.ycz.master.service.DeptService;
import com.ycz.master.service.EmpServcie;
import com.ycz.master.service.EmpTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * author zyc
 * Created on 2021/9/30 11:06
 */
@Controller
@RequestMapping("emp")
public class EmpController {

    @Autowired
    private EmpTypeService service;

    @Autowired
    private EmpServcie empServcie;

    @Autowired
    private DeptService deptService;

    @RequestMapping("/gettype")
    @ResponseBody
    public List<EmpType> getType(HttpSession session){
        List<EmpType> findall = service.findall();
        session.setAttribute("type",findall);
        return findall;
    }

//    @RequestMapping("/login")
//    @ResponseBody
//    public Emp login(@RequestParam String account, @RequestParam String password, HttpServletRequest request){
//        Emp login = empServcie.login(account, password);
//        if(login!=null){
//            request.getSession().setAttribute("login",login);
//        }
//        return login;
//    }


    //更改密码
    @RequestMapping("/changePassword")
    @ResponseBody
    public Boolean changePassword(String account,String password, String oldpassword){
        Boolean change = empServcie.change(account, password, oldpassword);
        return change;
    }
    //退出
    @RequestMapping("/logout")
    @ResponseBody
    public Boolean logout(HttpSession session){
        session.removeAttribute("login");
        return true;
    }


//    @RequestMapping("/finduser")
//    @ResponseBody
//    public PageInfo findallUser(@RequestParam(value = "pn",defaultValue = "1")Integer pn){
//        PageHelper.startPage(pn,5);
//
//        List<Emp> findallemp = empServcie.findallemp();
//        PageInfo pageinfo=new PageInfo(findallemp,5);
//        return pageinfo;
//    }
    @RequestMapping("/finduser")
    @ResponseBody
    public PageInfo findallUser(@RequestParam(value = "pn",defaultValue = "1")Integer pn){
        PageHelper.startPage(pn,5);

        List<EmpAndEmpTypeAndDept> findallemp = empServcie.findallemp();
        PageInfo pageinfo=new PageInfo(findallemp,5);
        return pageinfo;
    }


    //查询所有部门
    @RequestMapping("/finddept")
    @ResponseBody
    public List<Dept> findDept(HttpSession session){
        session.setAttribute("dept",deptService.finddept());
        return deptService.finddept();
    }

    //根据id查询员工
    @RequestMapping("empfindbyId/{id}")
    @ResponseBody
    public Emp findbyId(@PathVariable("id") Integer id){
        System.out.println(empServcie.findById(id));
        return empServcie.findById(id);
    }

    //更新员工信息
    @RequestMapping("/updata")
    @ResponseBody
    public Boolean updataemp(Emp emp){
        System.out.println(emp);
//        System.out.println(emp.getEmpId());
       return empServcie.updataemp(emp);
    }

    @RequestMapping("/insert")
    @ResponseBody
    public Boolean test(Emp emp){
        System.out.println(emp);
        int insertemp = empServcie.insertemp(emp);
//        System.out.println(insertemp);
//        System.out.println("sss");
        if(insertemp==1) {
            return true;
        }else {
            return false;
        }
    }
}
